package com.clown.test.one;

import javax.persistence.EntityManager;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Description
 * @Author ZL
 * @Date 2022-02-13 21:06
 * @Version 1.0
 */
public class MyJpaProxy implements InvocationHandler {
    EntityManager em;
    Class pojoClass;

    public MyJpaProxy(EntityManager em, Class pojoClass) {
        this.em = em;
        this.pojoClass = pojoClass;
    }

    // method 当前调用的方法   = findById
    // args 当前调用方法的参数  = 1
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // jpa统一实现类
        MyJpaRepository myJpaProxy = new MyJpaRepository(em, pojoClass);
        Method jpaMethod = myJpaProxy.getClass().getMethod(method.getName(), method.getParameterTypes());
        return jpaMethod.invoke(myJpaProxy, args);
    }
}
